package com.bdweb.threadPool;

import com.bdweb.dao.TaskDao;
import com.bdweb.model.TASK;
import com.bdweb.service.PowerAllService;
import com.bdweb.service.impl.MedailServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

import java.time.LocalDateTime;

import java.util.List;


@Service
public class openAllTime{
    @Autowired
    TaskDao taskDao;
    @Autowired
    PowerAllService powerAllService;
    @Autowired
    MedailServiceImpl medailService;
    public  void openall(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    List<TASK> list = taskDao.findAll();
                    // 获取当前系统时间

                    String nowSystemTime = String.valueOf(LocalDateTime.now()).replace("T", " ").substring(0, 19);
                    //获取每天的不带日期的时间
                    String everySystemTime = nowSystemTime.substring(11, 19);


                    // 获取单次开启时间
                    String start1 = "";
                    String start2 = "";


                    for (int i = 0; i < list.size(); i++) {
                        start1 = list.get(i).getStart1().replace("T", " ");
                        start2 = list.get(i).getStart2();




                        if(list.get(i).getTaskName().equals("一键开启")) {

                            // 一键开启
                            if (nowSystemTime.equals(start1)  ) {
                                System.out.println("我开启了1");

                                try {
                                    //开启电
                                    powerAllService.listPowerOpenAll();
                                    Thread.sleep(20000);
                                    //开启多媒体设备
                                    medailService.listMedailOpenAll();

                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }if(everySystemTime.equals(start2)){
                            System.out.println("我开启了2");
                            try {
                                //开启电
                                powerAllService.listPowerOpenAll();
                                Thread.sleep(20000);
                                //开启多媒体设备
                                medailService.listMedailOpenAll();
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }


                        if(list.get(i).getTaskName().equals("一键关闭")){

                            //一键关闭
                            if (nowSystemTime.equals(start1) ) {
                                System.out.println("我关闭了1");

                                try {


                                    medailService.listMedailCloseAll();
                                    Thread.sleep(20000);
                                    powerAllService.listPowerCloseAll();
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                            if(everySystemTime.equals(start2)){
                                System.out.println("我关闭了2");

                                try {

                                    medailService.listMedailCloseAll();
                                    Thread.sleep(20000);
                                    powerAllService.listPowerCloseAll();
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }




                    }
                }
            }
        }).start();

    }

}
